package org.thought;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

/**
 * The class for the "random" choice. It basically does the same
 * this as the Display class, only that it has a "next" button
 * which will generate another random prayer/saying.
 * 
 * @author Andrei Epure andrei.epure@cti.pub.ro
 */

public class Random extends Activity implements OnClickListener {
	Button close;
	Button next;
	TextView text;
	String[] elements;
	
	final int PRAYER = 1000;
	final int SAYING = 1001;
	
	public void onCreate(Bundle savedInstanceState){
		Intent intent;
		/*what we'll be writing*/
		int typeOfText;
		/*the index*/
		int index;
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.random);
		getWindow().setLayout(LayoutParams.FILL_PARENT, 
				LayoutParams.WRAP_CONTENT);
		
		close = (Button)findViewById(R.id.randomClose);
		next = (Button)findViewById(R.id.randomNext);
		text = (TextView)findViewById(R.id.randomText);
		
		intent = getIntent();
		typeOfText = intent.getIntExtra("text", PRAYER);
		
		elements = (typeOfText == PRAYER) 
				? getResources().getStringArray(R.array.prayers)
				: getResources().getStringArray(R.array.sayings);
		
	    index = IndexGenerator.random(elements.length);
				
		text.setText(elements[index]);
				
		close.setOnClickListener(this);
		next.setOnClickListener(this);
	}
	
	/**
	 * The button listener. It either closes the dialog or
	 * it generates another random element and displays it.
	 */
	@Override
	public void onClick(View v) {
		if(v == close){
			finish();
		}
		if(v == next){
			int index;
			index = IndexGenerator.random(elements.length);
			text.setText(elements[index]);
		}
	}


}